<template>
  <div>
    <!--    <el-row :gutter="10" class="mb8">-->
    <!--      <el-col :span="1.5">-->
    <el-select v-model="level1Id"
               placeholder="一级目录"
               @change="changeLevel1">
      <el-option
        v-for="(item, index) in this.level1List"
        :key="index"
        :label="item.label"
        :value="item.value"/>
    </el-select>


    <!--      </el-col>-->
    <!--      <el-col :span="1.5">-->
    <el-select v-model="level2Id"
               placeholder="二级类型"
               @change="changeLevel2">
      <el-option
        v-for="(item, index) in this.level2List"
        :key="index"
        :label="item.label"
        :value="item.value"/>
    </el-select>
    <!--      </el-col>-->
    <!--    </el-row>-->

  </div>
</template>

<script>

import {
  listTypeLevel1,
  listTypeLevel2,
  listSensorType
} from "@/api/system/sensorType"


export default {
  name: "typeSelect",
  props: {
    pLevel2Id: {
      type: Number
    }
  },
  data() {
    return {
      sensorTypeId: null,
      level1List: [],
      level2List: [],
      level2ListOption: [],
      level1Id: null,
      level2Id: null,
      sensorTypeList: []
    }
  },
  created() {
    // this.getList()
    this.getLevel1List()
  },
  methods: {

    // getList() {
    //
    //   this.sensorTypeList = []
    //
    //   listSensorType(null).then(response => {
    //
    //     this.sensorTypeList = response.rows
    //
    //   })
    // },

    getLevel1List() {

      console.log("有吗?：" + this.pLevel2Id)

      this.level1List = []
      listTypeLevel1().then(response => {
        for (let item of response.data) {
          let temp = {}
          temp['value'] = item.sensorTypeId
          temp['label'] = item.sensorTypeName
          this.level1List.push(temp)
        }
      })
    },
    getLevel2List(sensorTypeId) {
      this.level2List = []
      listTypeLevel2(sensorTypeId).then(response => {
        let i = 0
        for (let item of response.data) {
          if (i === 0) {
            this.level2Id = item.sensorTypeId
            this.changeLevel2(this.level2Id)
          }
          let temp = {}
          temp['value'] = item.sensorTypeId
          temp['label'] = item.sensorTypeName
          this.level2List.push(temp)
          ++i
        }
      })
    },
    changeLevel1(newValue) {
      this.getLevel2List(newValue)
    },
    changeLevel2(newValue) {
      console.log("二级类型是？" + newValue)
      let obj = {
        'sensorType': newValue,
      }
      this.$emit('changeType', obj)
    },
    parentHandler(e) {

      let sensorType = Number(e)

      this.sensorTypeList = []

      listSensorType(null).then(response => {

        this.sensorTypeList = response.rows

        console.log(this.sensorTypeList.length)

        let obj = this.sensorTypeList.find((item) => {
          return item.sensorTypeId === sensorType
        })
        console.log("ok")
        console.log(obj)
        this.level1Id = obj.parentId
        if (this.level1Id != null) {
          this.level2ListEcho(this.level1Id, sensorType)
        }
      })

    },
    level2ListEcho(sensorTypeId, sensorType) {
      this.level2List = []
      listTypeLevel2(sensorTypeId).then(response => {
        for (let item of response.data) {
          let temp = {}
          temp['value'] = item.sensorTypeId
          temp['label'] = item.sensorTypeName
          this.level2List.push(temp)
        }
        this.level2Id = sensorType
      })
    },
    addHandler() {
      this.level1Id = null
      this.level2Id = null
      this.getLevel1List()
    }
  }
}
</script>

<style scoped>

</style>
